<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @package  	PyroCMS
 * @subpackage  Categories
 * @category  	Module
 * @author  	PyroCMS Dev Team
 */
class Admin_Import extends Admin_Controller {

	/**
	 * The current active section
	 * @access protected
	 * @var int
	 */
	protected $section = 'import';
	protected $_importPath = '';
	/**
	 * The constructor
	 * @access public
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('stoepje_winners_m');
		$this->load->model('stoepje_vouchers_m');
		$this->load->model('stoepje_prizes_m');
		$this->lang->load('voucher');
		$this->config->load('voucher');
		$this->_importFolder = $this->config->item('upload_import_path');
		$this->_importPath = FCPATH . $this->config->item('upload_import_path');
		$this->template->set_partial('shortcuts', 'admin/partials/shortcuts')
						->append_metadata(js('stoepje.voucher.js', 'voucher'))
						->append_metadata(css('stoepje.voucher.css', 'voucher'));
	}
	
	/**
	 * Index method, lists all categories
	 * @access public
	 * @return void
	 */
	public function index()
	{
		/*
		$winnerList = $this->stoepje_winners_m->where('prize_id', 0)->get_all();
		//print_r($winnerList);
		if($winnerList){
			foreach ($winnerList as $winner){
				echo $winner->code;
				$voucher = $this->stoepje_vouchers_m->get_by(array('code' => $winner->code));
				echo $voucher->prize_id;
				$prize = $this->stoepje_prizes_m->get($voucher->prize_id);
				echo $prize->name;
				echo '<br/>';
				$this->stoepje_winners_m->update($winner->id, array(
					'prize_id'		=> $prize->id,
					'prize_name'	=> $prize->name
				));
			}
		}
		die();
		*/
		if(isset($_FILES['import']) && $_FILES['import']['name']){
			$csvFileName = $_FILES["import"]["name"];
			$resulst = move_uploaded_file($_FILES["import"]["tmp_name"],$this->_importPath . $csvFileName);
			if($resulst){
				$file= fopen($this->_importPath.$csvFileName, "r");
				setlocale(LC_ALL, 'ja_JP.UTF8');
				$i = 0;
				while (($info = fgetcsv($file, 0, ",")) !==false){
					if($i == 0){
						$i++;
						continue;
					}				
					$dataSet = array(
						'id' => $info[0],
						'code'=>$info[1],
						'prize_id'=>$info[2],
						'description'=>$info[3],
						'created_time'=>$info[4],
						'status'=>$info[5]
					);
					$id = $info[0];
					$this->stoepje_vouchers_m->insert($dataSet);
					$i ++;
				}
				$this->session->set_flashdata('success', sprintf(lang('stoepje_voucher_import_success_label'), $i));
			}else{
				$this->session->set_flashdata('error', $this->lang->line('stoepje_voucher_import_error_label'));
			}
			redirect('admin/voucher/import');
		}
	

		$this->template
			->title($this->module_details['name'], lang('stoepje_prize_list_title'))
			->build('admin/import', $this->data);
	}
	public function hToV(){
		$params = array('f_prize' =>4, 'f_status' => 0, 'limit' => 10000);//0
		$voucherList = $this->stoepje_vouchers_m->get_many_by($params);
		$i = 0;
		//print_r($voucherList);
		if($voucherList){
			foreach ($voucherList as $voucher){
				$this->stoepje_vouchers_m->update($voucher->id, array('prize_id' => 1));
				$i ++;
			}
		}
		echo $i." Done";
	}
	public function vToH(){
		$params = array('f_prize' =>1, 'f_status' => 0, 'limit' => 10000);//0
		$voucherList = $this->stoepje_vouchers_m->get_many_by($params);
		$i = 0;
		//print_r($voucherList);
		if($voucherList){
			foreach ($voucherList as $voucher){
				$this->stoepje_vouchers_m->update($voucher->id, array('prize_id' => 4));
				$i ++;
			}
		}
		echo $i." Done";
	}
}